﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace SilverRaport
{
    public partial class SqlPage : Page
    {
        MySilverRaporService.SilverRaporServiceClient MyService = new MySilverRaporService.SilverRaporServiceClient();
        public static int KullaniciID;
        public bool Update { get; set; }
        public SqlPage()
        {
            InitializeComponent();
            btnListe.Click += new RoutedEventHandler(btnListe_Click);
            btnKaydet.Click += new RoutedEventHandler(btnKaydet_Click);
            this.Loaded += new RoutedEventHandler(SqlPage_Loaded);

        }

        void SqlPage_Loaded(object sender, RoutedEventArgs e)
        {
            KullaniciGetir();
            MyService.RaporKaydetCompleted += new EventHandler<MySilverRaporService.RaporKaydetCompletedEventArgs>(MyService_RaporKaydetCompleted);
        }

        private void KullaniciGetir()
        {
            MyService.KullaniciGetirAsync(KullaniciID);
            MyService.KullaniciGetirCompleted += new EventHandler<MySilverRaporService.KullaniciGetirCompletedEventArgs>(MyService_KullaniciGetirCompleted);
        }

        void MyService_KullaniciGetirCompleted(object sender, MySilverRaporService.KullaniciGetirCompletedEventArgs e)
        {
            if (e.Result[0] != "false" || e.Result[0] != "Boş")
            {
                txtAdi.Text = e.Result[0];
                txtSoyadi.Text = e.Result[1];
                txtLimit.Text = e.Result[2];
            }
            else if (e.Result[0] == "false")
            {
                MessageBox.Show(e.Result[1]);
            }
        }

        void btnKaydet_Click(object sender, RoutedEventArgs e)
        {
            if (txtRaporInterval.Text != "" && txtRaporName.Text != "" && txtRaporSQL.Text != "")
            {
                if (Update)
                {
                    MyService.RaporKaydetAsync(txtRaporName.Text, txtRaporSQL.Text, int.Parse(txtRaporInterval.Text), KullaniciID, (byte)cmbRaporTipi.SelectedIndex, (byte)cmbGrafikTipi.SelectedIndex, RaporListele.RaporID);
                    Update = false;
                }
                else
                {
                    MyService.RaporKaydetAsync(txtRaporName.Text, txtRaporSQL.Text, int.Parse(txtRaporInterval.Text), KullaniciID, (byte)cmbRaporTipi.SelectedIndex, (byte)cmbGrafikTipi.SelectedIndex, 0);
                }
            }
        }

        void MyService_RaporKaydetCompleted(object sender, MySilverRaporService.RaporKaydetCompletedEventArgs e)
        {
            if (e.Result == 1)
                MessageBox.Show("SQL Limitiniz Dolmuştur..!!");
            else if (e.Result == 2)
                MessageBox.Show("Kayıt Başarılı.");
            else if (e.Result == 3)
                MessageBox.Show("Bir Hata Oluştu..!!");

            KullaniciGetir();
            txtRaporInterval.Text = "";
            txtRaporName.Text = "";
            txtRaporSQL.Text = "";
            cmbGrafikTipi.SelectedIndex = 0;
            cmbRaporTipi.SelectedIndex = 0;
        }

        void btnListe_Click(object sender, RoutedEventArgs e)
        {
            RaporListe raporListe = new RaporListe();
            raporListe.Show();

            raporListe.Closed += new EventHandler(raporListe_Closed);
        }

        void raporListe_Closed(object sender, EventArgs e)
        {
            if (RaporListele.RaporIsim != null)
            {
                txtRaporName.Text = RaporListele.RaporIsim;
                txtRaporSQL.Text = RaporListele.RaporSQL;
                txtRaporInterval.Text = RaporListele.RaporInterval.ToString();
                cmbRaporTipi.SelectedIndex = RaporListele.RaporTipi;
                cmbGrafikTipi.SelectedIndex = RaporListele.GrafikTipi;
            }
            KullaniciGetir();
            Update = true;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}
